var proProcess=new Object();

proProcess.projectId=$("#projectId").val();
proProcess.roleType=$("#roleType").val();

proProcess.init=function(basePath,page,page1,page2,projectType){
  proProcess.basePath=basePath;
  proProcess.page=page;
  proProcess.page1=page1;
  proProcess.page2=page2;
  proProcess.projectType=projectType;
  if(projectType == '1' || projectType == '4' || projectType == '5' || projectType == '7'){
    // 每个学员的培训进度
    proProcess.project_train_process();
      if(proProcess.roleType == "company_admin"){
          $(".com-select").removeClass("nav");
          window.groupTreeCheckbox.initData = proProcess.initData;
          window.groupTreeCheckbox.check = proProcess.check;
          groupTreeCheckbox.init();
          proProcess.initDepartment();
      }
  }
  if(projectType == '2' || projectType == '4' || projectType == '6' || projectType == '7'){
    // 练习排行记录
    proProcess.project_exercise_process();

      window.groupTreeCheckbox1.initData = proProcess.initData1;
      window.groupTreeCheckbox1.check = proProcess.check1;
      groupTreeCheckbox1.init();
      proProcess.initDepartment1();
  }
  if(projectType == '3' || projectType == '5' || projectType == '6' || projectType == '7'){
    // 考试成绩列表
    proProcess.project_exam_process();

      window.groupTreeCheckbox2.initData = proProcess.initData2;
      window.groupTreeCheckbox2.check = proProcess.check2;
      groupTreeCheckbox2.init();
      proProcess.initDepartment2();
  }
  //点击事件
  proProcess.click();

}

    // 每个学员的培训进度
    proProcess.project_train_process = function(){
        var page_size=10;
        var train_process_url = appPath + "/statistics/project_statistics_list";
        proProcess.page.init("select_train_form", train_process_url, "project_train", "project_train_page", 1, page_size);
        proProcess.page.goPage(1);
        proProcess.page.list = function(dataList){
            var len = dataList.length;
            var inner = "", item;
            var roleId = "";
            var roleName = "/";
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td>' + (i+1) + '</td>';
                inner +='<td>'+item['user_name']+'</td>';
                roleId = item['role_id'];
                if("-1" != roleId){
                    roleName = item['role_name'];
                }
                inner +='<td>'+roleName+'</td>';
                if(item['company_name']){
                    inner += '<td>'+item['company_name']+'</td>';
                }else{
                    inner += '<td>/</td>';
                }

                inner += '<td>'+item['dept_name']+'</td>';
                inner += '<td>'+TimeUtil.getHouAndMinAndSec(item['requirement_studytime']*60)+'</td>';
                inner += '<td>'+TimeUtil.getHouAndMinAndSec(item['total_studytime'])+'</td>';
                inner += '<td>'+item['train_process']+'</td>';
                inner +='<td>';
                inner +='<a  onclick=\"proProcess.train_record(\'' + item.project_id+ '\',\'' + item.user_id+ '\');\" class=\"a a-info\">培训详情</a>';
                inner +='</td>';
                inner += '</tr>';
            }
            return inner;
        }
    };

    //练习排行情况
    proProcess.project_exercise_process=function () {
      var page_size=10;
      var exercise_process_url = appPath + "/exercise/showProjectExerciseOrder";
      proProcess.page1.init("select_exercise_form", exercise_process_url, "project_exercise", "project_exercise_page", 1, page_size);
      proProcess.page1.goPage(1);
      proProcess.page1.list = function(dataList){
            var len = dataList.length;
            var inner = "", item;
            var roleId = "";
            var roleName = "/";
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner +='<td>'+item['rownum']+'</td>';
                inner +='<td>'+item['userName']+'</td>';
                roleId = item['roleId'];
                if("-1" != roleId){
                    roleName = item['roleName'];
                }
                inner +='<td>'+ roleName +'</td>';
                inner +='<td>'+item['companyName']+'</td>';
                inner +='<td>'+item['deptName']+'</td>';
                inner +='<td>'+item['totalQuestion']+'</td>';
                inner +='<td>'+item['yetAnswered']+'</td>';
                inner +='<td>'+item['correctAnswered']+'</td>';
                inner +='<td>'+item['correctRate']+'%</td>';
                inner +='<td>'+TimeUtil.getHouAndMinAndSec(item['answerStudyTime'])+'</td>';
                inner +='<td>';
                inner +='<a  onclick=\"proProcess.exercise_record(\'' + item.projectId+ '\',\'' + item.userId+ '\');\" class=\"a a-publish\">练习详情</a>';
                inner +='</td>';
                inner += '</tr>';
            }
            return inner;
        }
    }

    //考试成绩列表
    proProcess.project_exam_process=function () {
      var page_size=10;
      var exam_process_url = appPath + "/statistics/project_statistics_list1";
      proProcess.page2.init("select_exam_form", exam_process_url, "project_exam", "project_exam_page", 1, page_size);
      proProcess.page2.goPage(1);
      proProcess.page2.list = function(dataList){
        var len = dataList.length;
        var inner = "", item;
        var exam_score_info = "";
        var roleId = "";
        var roleName = "/";
          // 组装数据
        for(var i=0; i< len; i++) {
          item = dataList[i];
          inner += '<tr >';
          inner += '<td>' + (i+1) + '</td>';
          inner +='<td>'+item['user_name']+'</td>';
            roleId = item['role_id'];
            if("-1" != roleId){
                roleName = item['role_name'];
            }
          inner +='<td>'+roleName+'</td>';
            if(item['company_name']){
                inner +='<td>'+item['company_name']+'</td>';
            }else{
                inner += '<td>/</td>';
            }
          inner +='<td>'+item['dept_name']+'</td>';
          inner +='<td>'+item['exam_score']+'</td>';
          inner +='<td>'+item['subjective_score']+'</td>';
          inner +='<td>'+item['total_score']+'</td>';
          inner +='<td>'+item['exam_Time']+'</td>';   //考试耗时
          if(item['exam_status'] == '1'){
            inner +='<td><span class="text-gray">未考试</span></td>';
          }
          if(item['exam_status'] == '2'){
            inner +='<td><span class="text-green">合格</span></td>';
          }
          if(item['exam_status'] == '3'){
            inner +='<td><span class="text-red">不合格</span></td>';
          }
          inner +='<td>';
          if(item['exam_status'] == '1'){
            inner += '<a  class=\"a\">答题记录</a>';
          }else{
            inner += '<a  onclick=\"proProcess.exam_record(\'' + item.exam_no+ '\');\" class=\"a a-view\">答题记录</a>';
          }
          inner +='</td>';
          inner += '</tr>';
        }
        return inner;
      }
    }
  /* 查看培训记录*/
  proProcess.train_record = function(projectId,userId) {
    window.location.href = appPath + "/admin/projectCourseInfo/project_course_entry?projectId=" + projectId + "&userId=" + userId + "&type=1";
  }
  /* 查看练习记录*/
  proProcess.exercise_record = function(projectId,userId) {
    window.location.href = appPath + "/admin/projectCourseInfo/project_course_entry?projectId=" + projectId + "&userId=" + userId + "&type=2";
  }
  /* 查看答题记录*/
  proProcess.exam_record = function(examNo){
    common.goto_student_exam_answer_view(examNo);
  }

  //点击事件
  proProcess.click = function(){
    //搜索
    $("#train_search").click(function(){
      $("#userName_train").val($.trim($("#userName_train").val()));
      $("#companyIds_train").val($.trim($("#department_select").val()));
      proProcess.project_train_process();
    });
    //全部
    $("#train_all").click(function(){
      $("#userName_train").val("");
      $("#roleId").val("");
      $("#companyIds_train").val("");
      $('.select2-selection__rendered').empty();
      proProcess.project_train_process();
    });
    //搜索
    $("#exercise_search").click(function(){
      $("#userName_exercise").val($.trim($("#userName_exercise").val()));
      $("#companyIds_exercise").val($.trim($("#department_select1").val()));
      proProcess.project_exercise_process();
    });
    //全部
    $("#exercise_all").click(function(){
      $("#userName_exercise").val("");
      $("#companyIds_exercise").val("");
        $('.select2-selection__rendered').empty();
      proProcess.project_exercise_process();
    });
    //搜索
    $("#exam_search").click(function(){
      $("#userName_exam").val($.trim($("#userName_exam").val()));
      $("#companyIds_exam").val($.trim($("#department_select2").val()));
      proProcess.project_exam_process();
    });
    //全部
    $("#exam_all").click(function(){
      $("#userName_exam").val("");
      $("#companyIds_exam").val("");
        $('.select2-selection__rendered').empty();
      proProcess.project_exam_process();
    });

    proProcess.sort = function(type){
      if(type == '1'){
        $("#sort1").addClass("active");
        $("#sort2,#sort3").removeClass("active");
        $("#orderType").val("1");
      }
      if(type == '2'){
        $("#sort2").addClass("active");
        $("#sort1,#sort3").removeClass("active");
        $("#orderType").val("2");
      }
      if(type == '3'){
        $("#sort3").addClass("active");
        $("#sort1,#sort2").removeClass("active");
        $("#orderType").val("3");
      }
      proProcess.project_exercise_process();
    }

      $('#project_role_list').on("click", "span", function(){
          $('#project_role_list').find("span").removeClass("active");
          $(this).addClass("active");
          proProcess.change_role(this, "roleId");
      });

      $('#user_project_status').on("click", "span", function(){
          $('#user_project_status').find("span").removeClass("active");
          $(this).addClass("active");
          proProcess.change_role(this, "proUserStatus");
      });
}


proProcess.export_train_info = function(){
    var params = $("#select_train_form").serialize();
    window.location.href = appPath + '/statistics/exportTrainInfo?'+ params;
};

proProcess.export_exercise_info = function(){
    var params = $("#select_exercise_form").serialize();
    window.location.href = appPath + '/exercise/exportExerciseInfo?'+ params;
};

proProcess.export_exam_info = function(){
    var params = $("#select_exam_form").serialize();
    window.location.href = appPath + '/statistics/exportExamInfo?'+ params;
};
// 切换角色更新列表
proProcess.change_role = function (obj, id) {
    var roleId = $(obj).attr("data-value");
    $("#"+id).val(roleId);
    $("#train_"+id).val(roleId);
    $("#exam_"+id).val(roleId);
    // 加载各个角色下的培训项目进度情况
    proProcess.getTrainProcess(roleId,proProcess.projectId);
    // 加载各个角色下的培训情况
    proProcess.project_train_process();
    // 加载各个角色下的练习排行情况
    proProcess.project_exercise_process();
    // 加载各个角色下的考试总体情况
    proProcess.getExamProcess(roleId,proProcess.projectId);
    // 加载各个角色下的考试情况
    proProcess.project_exam_process();
};


proProcess.getTrainProcess = function (roleId,projectId) {
    $.ajax({
        url: appPath + '/admin/project/getTrainProcess',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {'projectId': projectId,'roleId':$("#roleId").val(), 'proUserStatus':$("#proUserStatus").val()},
        success: function (data) {
            var code = data.code;
            var result = data.result;
            var trainProjectProcessMap = result.trainProjectProcessMap;
            $("#trainProcess tr:eq(0) td:eq(1)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_count : 0);
            $("#trainProcess tr:eq(0) td:eq(2)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_50_count : 0);
            $("#trainProcess tr:eq(0) td:eq(3)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_50_100_count : 0);
            $("#trainProcess tr:eq(0) td:eq(4)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_100_count : 0);
            $("#trainProcess tr:eq(0) td:eq(5)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_sum_count + "人" : 0 + "人" );

            $("#trainProcess tr:eq(1) td:eq(1)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_percent : 0);
            $("#trainProcess tr:eq(1) td:eq(2)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_50_percent : 0);
            $("#trainProcess tr:eq(1) td:eq(3)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_50_100_percent : 0);
            $("#trainProcess tr:eq(1) td:eq(4)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_100_percent : 0);
            $("#trainProcess tr:eq(1) td:eq(5)").text(null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_sum_percent : 0);

        }
    });
};

proProcess.getExamProcess = function (roleId,projectId) {
    $.ajax({
        url: appPath + '/admin/project/getExamProcess',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {'projectId': projectId,'roleId':$("#roleId").val(), 'proUserStatus':$("#proUserStatus").val()},
        success: function (data) {
            var code = data.code;
            var result = data.result;
            var examProcessMap = result.examProcessMap;
            $("#examProcess tr:eq(0) td:eq(0)").text(null != examProcessMap ? examProcessMap.userCount : 0);
            $("#examProcess tr:eq(0) td:eq(1)").text(null != examProcessMap ? examProcessMap.examCount : 0);
            $("#examProcess tr:eq(0) td:eq(2)").text(null != examProcessMap ? examProcessMap.examPercent : "0%");
            $("#examProcess tr:eq(0) td:eq(3)").text(null != examProcessMap ? examProcessMap.passCount : 0);
            $("#examProcess tr:eq(0) td:eq(4)").text(null != examProcessMap ? examProcessMap.passPercent  : "0%");
        }
    });
};




